package com.grieous.wow.web.controller;

import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import com.grieous.wow.biz.ContentBiz;
import com.grieous.wow.entity.Domain;
import com.grieous.wow.entity.PageModel;
import com.grieous.wow.entity.User;

@Controller
@RequestMapping("/domain")
public class DomainController {

	@Resource
	private ContentBiz contentBiz;

	@ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	// 域名管理
	@RequestMapping("/manage")
	public String domain(HttpServletRequest request,
			HttpServletResponse response) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			Domain domain = contentBiz.getMyDomain();
			request.setAttribute("domain", domain);
			return "admin/content/domain";
		}
		return null;
	}

	@RequestMapping("/save")
	public String changeDomain(HttpServletRequest request,
			HttpServletResponse response, Domain domain) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User user = (User) request.getSession().getAttribute("admin");
			if (user != null) {
				if(contentBiz.saveDomain(domain))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 友情链接管理
	@RequestMapping("/link/manage")
	public String linkManager(Integer page, Integer pageSize,
			HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			PageModel pageModel = contentBiz.getFooterLink(pageSize, page);
			request.setAttribute("pageModel", pageModel);
			return "admin/content/link";
		}
		return null;
	}

	// 增加友情链接
	@RequestMapping("/link/add")
	public String addLink(HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			return "admin/content/saveLink";
		}
		return null;
	}

	// 编辑友情链接
	@RequestMapping("/link/edit")
	public String editLink(int id, Model model) {
		Domain domain = contentBiz.getDomainById(id);
		model.addAttribute(domain);
		return "admin/content/saveLink";
	}

	@RequestMapping("/link/save")
	public String saveLink(HttpServletRequest request,
			HttpServletResponse response, Domain domain) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if(contentBiz.saveDomain(domain))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/link/delete")
	public String delLink(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if(contentBiz.deleteDomain(ids))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
